package com.cuiweiyou.happypaint.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by macpro on 2018/3/5.
 */

public class SPUtil {
    private static SPUtil instance;
    private SharedPreferences sp;

    private SPUtil() {
    }

    public static SPUtil getInstance() {
        if (null == instance) {
            synchronized (SPUtil.class) {
                if (null == instance) {
                    instance = new SPUtil();
                }
            }
        }
        return instance;
    }

    public void init(Context context) {
        if (null == sp) {
            sp = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
        }
    }

    public void save(String key, String value) {
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(key, value);
        edit.commit();
    }

    public void save(String key, Boolean value) {
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    public void save(String key, Integer value) {
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt(key, value);
        edit.commit();
    }

    public void save(String key, Float value) {
        SharedPreferences.Editor edit = sp.edit();
        edit.putFloat(key, value);
        edit.commit();
    }

    public void save(String key, Long value) {
        SharedPreferences.Editor edit = sp.edit();
        edit.putLong(key, value);
        edit.commit();
    }

    public String readString(String key) {
        return sp.getString(key, "");
    }

    public Boolean readBoolean(String key) {
        return sp.getBoolean(key, false);
    }

    public Boolean readLockApp( ) {
        return sp.getBoolean("lock_app", true);
    }

    public Float readFloat(String key) {
        return sp.getFloat(key, 0f);
    }

    public Integer readInt(String key) {
        return sp.getInt(key, -1);
    }

    public Long readLong(String key) {
        return sp.getLong(key, 0l);
    }

    public void clear(){
        SharedPreferences.Editor edit = sp.edit();
        edit.clear();
        edit.commit();
    }
}

